package nsu.edu.zsq.controller.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import nsu.edu.zsq.bean.ResitExam;
import nsu.edu.zsq.bean.ResitExamStudent;
import nsu.edu.zsq.bean.ResitExamTeacher;
import nsu.edu.zsq.bean.Student;
import nsu.edu.zsq.bean.Teacher;
import nsu.edu.zsq.common.JsonData;
import nsu.edu.zsq.param.PageQuery;
import nsu.edu.zsq.service.sys.ResitExamService;

/** 
* Description: 补考信息管理的controller类<br>
*/
@RequestMapping("/sys/resitExam")
@Controller
public class ResitExamController {

    @Autowired
    private ResitExamService resitExamService;
    
    /**
     * 获取补考课程列表
     * @param pageQuery 分页信息
     * @return JsonData
     */ 
    @RequestMapping("/listResitExam")
    @ResponseBody
    public JsonData listResitExamWithPage(PageQuery pageQuery) {
        PageInfo<ResitExam> listResitExam = resitExamService.listResitExamWithPage(pageQuery);
        return JsonData.success(listResitExam);
    }
    
    /**
     * 获取所有补考的课程（即不及格的课程，只展示必修课程）
     * @return JsonData
     */
    @RequestMapping("/listStuScore")
    @ResponseBody
    public JsonData listStuScore() {
        List<HashMap<String, String>> listStuScore = resitExamService.listStuScore();
        return JsonData.success(listStuScore);
    }
    
    /**
     * 保存需要补考课程到补考表中
     * @param courseNos 课程代码数组
     * @param majorNos 专业代码数组
     * @return JsonData
     */
    @RequestMapping("/saveCourseToResitExam")
    @ResponseBody
    public JsonData saveCourseToResitExam(@RequestParam("courseNos[]")String[] courseNos, @RequestParam("majorNos[]")String[] majorNos) {
        resitExamService.saveCourseToResitExam(courseNos, majorNos);
        return JsonData.success();
    }
    
    /**
     * 单个/批量删除补考课程
     * @param resitExamIds 补考课程id数组
     * @return JsonData
     */
    @RequestMapping("/delResitExam")
    @ResponseBody
    public JsonData delResitExam(@RequestParam("resitExamIds[]")int[] resitExamIds) {
        resitExamService.deleteCourseFromResitExam(resitExamIds);
        return JsonData.success();
    }
    
    /**
     * 跳转到补考课程时间设置页面
     * @param resitExamId 补考课程id
     * @param model 
     * @return  manager/exam/resit/resitExam_editTime.html
     */
    @RequestMapping("/toEditResitExamTime")
    public String toEditResitExamTime(@RequestParam("resitExamId")int resitExamId, Model model) {
        ResitExam resitExam = resitExamService.getResitExam(resitExamId);
        model.addAttribute("resitExam", resitExam);
        return "manager/exam/resit/resitExam_editTime.html";
    }
    
    /**
     * 编辑补考时间
     * @param resitExam 补考课程信息
     * @return JsonData
     */
    @RequestMapping("/updateResitExamTime")
    @ResponseBody
    public JsonData updateResitExamTime(ResitExam resitExam) {
        resitExamService.updateResitExam(resitExam);
        return JsonData.success();
    }
    
    /**
     * 跳转到考试安排页面
     * @param resitExamId 补考id
     * @param model
     * @return String manager/exam/resit/resitExam_manager.html
     */
    @RequestMapping("/toResitExam_manager")
    public String toResitExam_manager(@RequestParam("resitExamId")int resitExamId, Model model) {
        model.addAttribute("resitExamId", resitExamId);
        return "manager/exam/resit/resitExam_manager.html";
    }
    
    /**
     * 获取一条补考信息，携带考试教室信息
     * @param resitExamId 补考id
     * @return JsonData
     */
    @RequestMapping("/getResitExamWithRoom")
    @ResponseBody
    public JsonData getResitExamWithRoom(@RequestParam("resitExamId")int resitExamId) {
        Map<String, Object> resitExamWithRoom = resitExamService.getResitExamWithRoom(resitExamId);
        return JsonData.success(resitExamWithRoom);
    }
    
    /**
     * 保存补考的考试教室
     * @param resitExam 补考信息
     * @return JsonData 
     */
    @RequestMapping("/saveResitExamRoom")
    @ResponseBody
    public JsonData saveResitExamRoom(ResitExam resitExam) {
        resitExamService.saveExamRoom(resitExam);
        return JsonData.success();
    }
    
    /**
     * 根据补考id，删除考试教室
     * @param resitExamId 补考id
     * @param roomNames 考试教室名称数组
     * @return JsonData
     */
    @RequestMapping("/delResitExamRoom")
    @ResponseBody
    public JsonData delResitExamRoom(@RequestParam("resitExamId")int resitExamId, @RequestParam("roomNames[]")String[] roomNames) {
        resitExamService.delExamRoom(resitExamId, roomNames);
        return JsonData.success();
    }
    
    // ===========================================监考教师========================================
    
    /**
     * 跳转到监考教师页面
     * @param resitExamId 补考id
     * @param roomName 监考教室
     * @param model
     * @return String manager/exam/resit/resitExam_manager_examTeacher.html
     */
    @RequestMapping("/toEditExamTeacher")
    public String toEditExamTeacher(@RequestParam("resitExamId")int resitExamId, @RequestParam("roomName")String roomName
            , Model model) {
        model.addAttribute("resitExamId", resitExamId);
        model.addAttribute("roomName", roomName);
        return "manager/exam/resit/resitExam_manager_examTeacher.html";
    }
    
    /**
     * 获取该补考课程的监考教师
     * @param resitExam 补考信息
     * @return JsonData
     */
    @RequestMapping("/listResitExamTeacher")
    @ResponseBody
    public JsonData listResitExamTeacher(ResitExamTeacher resitExamTeacher) {
        List<ResitExamTeacher> listResitExamTeacher = resitExamService.listResitExamTeacher(resitExamTeacher);
        return JsonData.success(listResitExamTeacher);
    }
    
    /**
     * 跳转到添加监考教师页面
     * @param resitExamId 补考id
     * @param roomName 监考教室
     * @param model
     * @return String manager/exam/resit/resitExam_manager_addExamTeacher.html
     */
    @RequestMapping("/toAddExamTeacher")
    public String toAddExamTeacher(@RequestParam("resitExamId")int resitExamId, @RequestParam("roomName")String roomName
            , Model model) {
        model.addAttribute("resitExamId", resitExamId);
        model.addAttribute("roomName", roomName);
        return "manager/exam/resit/resitExam_manager_addExamTeacher.html";
    }
    
    /**
     * 获取当前补考课程所在院系的所有教师列表
     * @param resitExamId 补考id
     * @param pageQuery 分页信息
     * @return JsonData
     */
    @RequestMapping("/listTeacher")
    @ResponseBody
    public JsonData listTeacher(@RequestParam("resitExamId")int resitExamId, PageQuery pageQuery) {
        ResitExam resitExam = resitExamService.getResitExam(resitExamId);
        PageInfo<Teacher> listTeacher = resitExamService.listTeacherByMajorNoWithPage(resitExam.getMajorNo(), pageQuery);
        return JsonData.success(listTeacher);
    }
    
    /**
     * 添加补考的监考教师信息
     * @param resitExamTeacher 监考教师信息
     * @param teacherNos 教师编号数组
     * @return JsonData
     */ 
    @RequestMapping("/saveTeacherToResitExam")
    @ResponseBody
    public JsonData saveTeacherToResitExam(ResitExamTeacher resitExamTeacher, @RequestParam("teacherNos[]") String[] teacherNos) {
        resitExamService.saveTeacherToResitExam(resitExamTeacher, teacherNos);
        return JsonData.success();
    }
    
    /**
     * 单个/批量删除补考的监考教师信息
     * @param resitExamTeacherIds 监考教师id
     * @return JsonData
     */
    @RequestMapping("/delResitExamTeacher")
    @ResponseBody
    public JsonData delResitExamTeacher(@RequestParam("resitExamTeacherIds[]") int[] resitExamTeacherIds) {
        resitExamService.delResitExamTeacher(resitExamTeacherIds);
        return JsonData.success();
    }
    
    // ===========================================考试学生========================================
    
    /**
     * 跳转到考试学生页面
     * @param resitExamId 补考id
     * @param roomName 监考教室
     * @param model
     * @return String manager/exam/resit/resitExam_manager_examStudent.html
     */
    @RequestMapping("/toEditExamStudent")
    public String toEditExamStudent(@RequestParam("resitExamId")int resitExamId, @RequestParam("roomName")String roomName
            , Model model) {
        model.addAttribute("resitExamId", resitExamId);
        model.addAttribute("roomName", roomName);
        return "manager/exam/resit/resitExam_manager_examStudent.html";
    }
    
    /**
     * 获取该补考课程的考试学生
     * @param resitExam 补考信息
     * @return JsonData
     */
    @RequestMapping("/listResitExamStudent")
    @ResponseBody
    public JsonData listResitExamStudent(ResitExamStudent resitExamStudent) {
        List<ResitExamStudent> listResitExamStudent = resitExamService.listResitExamStudent(resitExamStudent);
        return JsonData.success(listResitExamStudent);
    }
    
    /**
     * 跳转到添加考试学生页面
     * @param resitExamId 补考id
     * @param roomName 考试教室
     * @param model
     * @return String manager/exam/resit/resitExam_manager_addExamStudent.html
     */
    @RequestMapping("/toAddExamStudent")
    public String toAddExamStudent(@RequestParam("resitExamId")int resitExamId, @RequestParam("roomName")String roomName
            , Model model) {
        model.addAttribute("resitExamId", resitExamId);
        model.addAttribute("roomName", roomName);
        return "manager/exam/resit/resitExam_manager_addExamStudent.html";
    }
    
    /**
     * 获取当前补考课程中需要补考的学生（该课程不及格的学生）
     * @param resitExamId 补考id
     * @param pageQuery 分页信息
     * @return JsonData
     */
    @RequestMapping("/listStudent")
    @ResponseBody
    public JsonData listStudent(@RequestParam("resitExamId")int resitExamId, PageQuery pageQuery) {
        ResitExam resitExam = resitExamService.getResitExam(resitExamId);
        PageInfo<Student> listStudent = resitExamService.listStudentByCourseNoWithPage(resitExam.getCourseNo(), pageQuery);
        return JsonData.success(listStudent);
    }
    
    /**
     * 添加补考的考试学生信息
     * @param resitExamStudent 考试学生信息
     * @param stuNos 学号数组
     * @return JsonData
     */ 
    @RequestMapping("/saveStudentToResitExam")
    @ResponseBody
    public JsonData saveStudentToResitExam(ResitExamStudent resitExamStudent, @RequestParam("stuNos[]") String[] stuNos) {
        resitExamService.saveStudentToResitExam(resitExamStudent, stuNos);
        return JsonData.success();
    }
    
    /**
     * 单个/批量删除补考的考试学生信息
     * @param resitExamStudentIds 考试学生id
     * @return JsonData
     */
    @RequestMapping("/delResitExamStudent")
    @ResponseBody
    public JsonData delResitExamStudent(@RequestParam("resitExamStudentIds[]") int[] resitExamStudentIds) {
        resitExamService.delResitExamStudent(resitExamStudentIds);
        return JsonData.success();
    }
    
    /**
     * 补考学生自动排座位号
     * @param resitExamStudent 补考学生信息
     * @return JsonData
     */
    @RequestMapping("/autoSeatNumber")
    @ResponseBody
    public JsonData autoSeatNumber(ResitExamStudent resitExamStudent) {
        resitExamService.autoSeatNumber(resitExamStudent);
        return JsonData.success();
    }
}
